export { }


//类型断言
const arr = [1, 2, 3]
const res = arr.find(i => i == 3)
// onst c = res*res c//res可能为 number|undefined
const num1 = res as number  //或者 num1 = <number>res  //jsx中不可用
const c = num1 * num1

//接口
interface Post {
    title: string;  //后面可以加逗号、冒号或者什么都不加
    context: string,
    readonly summary: string;
    [key: number]: Array<number | string>
}
const interObj: Post = {
    title: 'sss',
    context: 'xxx',
    summary: 'readonly',
    666: [2, '']
}
interObj.context = 'sss'
// interObj.summary = 'sss' //会报错
interObj[333] = [33, 66]

//class 类
class Person {
    protected names: string
    private age: number //只能在当前内部使用
    love: string = 'sun'
    constructor(names: string, age: number) {
        this.names = names
        this.age = age
    }
    static creat(names: string, age: any) {//只能被当前Person使用
        return new Person(names, age)
    }
}
// Person.names //报错
// Person.age //报错
// Person.creat() //不报错
let n = Person.creat('zhangsan', 18)
class dog extends Person {
    constructor(names: string, age: number, love: string) {
        super(names, age)
        this.names = names
        // this.age = age //报错
    }
}
//implements
interface EatAndRun {
    eat(food: string): void
    run(ditance: number): void
}
interface Sleep {
    sleep(): void
}
class Animal11 implements EatAndRun, Sleep {
    eat(food: string) {
        ///
    }
    run(distace: number) {
        ///
    }
    sleep() { }
}

// 抽象类
abstract class Animals {
    eat(food: string): void {
        console.log(food)
    }
    abstract run(distace: number): void
}
class Dog extends Animals {  //点击灯泡自动实现抽象类
    run(distace: number): void {
        throw new Error("Method not implemented.");
    }

}
// 泛型
function creatArray<T>(length: number, value: T): T[] {
    return Array<T>(length).fill(value)
}
creatArray<number>(3, 100)

type A = 'a' | 'b'
const obj: A = 'a'
type B = {
    [key in A]: String
}

/*  实现connect类型工具 */
interface Init {
    count: number,
    message: string,
    asyncMethod<T, U>(input: Promise<T>): Promise<Action<U>>
    syncMethod<T, U>(action: Action<T>): Action<U>
}
interface Action<T> {
    payload?: T,
    type: string
}
type Result = {
    asyncMethod<T, U>(input: T): Action<U>,
    syncMethod<T, U>(action: T): Action<U>;
}
//移除非函数属性的类型工具 输出: 'asyncMethod' | 'syncMethod'
type RemoveNonFunctionProps<T> = {
    [K in keyof T]: T[K] extends Function ? K : never;
}[keyof T]

//将只包含函数属性的类型pick出来 Pick<Init, 'asyncMethod' | 'syncMethod'>
// type PickFunction<T> = Extract<keyof T, RemoveNonFunctionProps<T>> //'asyncMethod' | 'syncMethod'
type PickFunction<T> = Pick<T, RemoveNonFunctionProps<T>>

//使用infer 转换函数
type TransformMethod<T> = T extends (input: Promise<infer U>) => Promise<Action<infer S>>
    ? (input: U) => Action<S> :
    T extends (action: Action<infer U>) => Action<infer S>
    ? (action: U) => Action<S> : never

type ConnectAll<T> = {
    [K in keyof T]: TransformMethod<T[K]>
}

type Connect<T> = ConnectAll<PickFunction<T>>

type R = Connect<Init>

const result: R = {
    asyncMethod<T, U>(input: T) {
        return {
            type: 'ss'
        }
    },
    syncMethod<T, U>(input: T) {
        return {
            type: 'ss'
        }
    }
}